package com.software.messagecenter.controller;

import com.software.messagecenter.entity.Message;
import com.software.messagecenter.entity.User;
import com.software.messagecenter.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    //查询某人聊天列表人员
    @GetMapping("/findReceiverById/{id}")
    public List<User> findReceiverById(@PathVariable String id){
        List<User> userList = messageService.findReceiverBySendId(id);
        return userList;
    }

    //查询某两人聊天信息
    @GetMapping("/findMessagesBySenderIdAndReceiverId/{senderId}/{receiverId}")
    public List<Message> findMessagesBySendIdAndReceiveId(@PathVariable("senderId") String senderId, @PathVariable("receiverId") String receiverId){
        List<Message> messageList = messageService.findMessagesBySenderIdAndReceiverId(senderId,receiverId);
        return messageList;
    }

    //发送消息
    @PostMapping("/addMessage")
    public Message addClassMessage(@RequestBody Message message){
        System.out.println(message);
        Message create = new Message();
        create.setMessageId(UUID.randomUUID().toString());
        create.setSenderId(message.getSenderId());
        create.setReceiverId(message.getReceiverId());
        create.setMessageDetail(message.getMessageDetail());
        Date date = new Date();
        create.setSendTime(date);
        messageService.addClassMessage(create);
        return create;
    }




}
